package com.alex.springbootdemo.service.impl;

import com.alex.springbootdemo.mapper.customize.UmsRoleDao;
import com.alex.springbootdemo.mapper.mbg.UmsRoleMapper;
import com.alex.springbootdemo.mapper.mbg.UmsRoleMenuRelationMapper;
import com.alex.springbootdemo.mapper.mbg.UmsRoleResourceRelationMapper;
import com.alex.springbootdemo.model.pojo.*;
import com.alex.springbootdemo.service.UmsRoleService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.Date;
import java.util.List;

/**
 * 后台角色管理Service实现类
 */
@Service
public class UmsRoleServiceImpl implements UmsRoleService {
    @Autowired
    private UmsRoleMapper roleMapper;
    @Autowired
    private UmsRoleMenuRelationMapper roleMenuRelationMapper;
    @Autowired
    private UmsRoleResourceRelationMapper roleResourceRelationMapper;
    @Autowired
    private UmsRoleDao roleDao;

    @Override
    public int create(UmsRole role) {
        role.setCreateTime(new Date());
        role.setAdminCount(0);
        role.setSort(0);
        return roleMapper.insert(role);
    }

    @Override
    public int update(Long id, UmsRole role) {
        role.setId(id);
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public int delete(List<Long> ids) {
        Weekend<UmsRole> weekend = Weekend.of(UmsRole.class);
        weekend.weekendCriteria()
                .andIn(UmsRole::getId, ids);
        int count = roleMapper.deleteByExample(weekend);
        return count;
    }

    @Override
    public List<UmsRole> list() {
        return roleMapper.selectAll();
    }

    @Override
    public List<UmsRole> list(String name, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        Weekend<UmsRole> weekend = Weekend.of(UmsRole.class);
        WeekendCriteria<UmsRole, Object> weekendCriteria = weekend.weekendCriteria();
        if (StringUtils.isNotEmpty(name)) {
            weekendCriteria.andLike(UmsRole::getName, name);
        }
        return roleMapper.selectByExample(weekend);
    }

    @Override
    public List<UmsMenu> getMenuListByAdminId(Long adminId) {
        return roleDao.getMenuListByAdminId(adminId);
    }

    @Override
    public List<UmsMenu> listMenuByRoleId(Long roleId) {
        return roleDao.getMenuListByRoleId(roleId);
    }

    @Override
    public List<UmsResource> listResourceByRoleId(Long roleId) {
        return roleDao.getResourceListByRoleId(roleId);
    }

    @Override
    public int allotMenuToRole(Long roleId, List<Long> menuIds) {
        //先删除原有关系
        Weekend<UmsRoleMenuRelation> weekend = Weekend.of(UmsRoleMenuRelation.class);
        weekend.weekendCriteria()
                .andEqualTo(UmsRoleMenuRelation::getRoleId, roleId);
        roleMenuRelationMapper.deleteByExample(weekend);
        //批量插入新关系
        for (Long menuId : menuIds) {
            UmsRoleMenuRelation relation = new UmsRoleMenuRelation();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            roleMenuRelationMapper.insertSelective(relation);
        }
        return menuIds.size();
    }

    @Override
    public int allotResourceToRole(Long roleId, List<Long> resourceIds) {
        //先删除原有关系
        Weekend<UmsRoleResourceRelation> weekend = Weekend.of(UmsRoleResourceRelation.class);
        weekend.weekendCriteria()
                .andEqualTo(UmsRoleResourceRelation::getRoleId, roleId);
        roleResourceRelationMapper.deleteByExample(weekend);
        //批量插入新关系
        for (Long resourceId : resourceIds) {
            UmsRoleResourceRelation relation = new UmsRoleResourceRelation();
            relation.setRoleId(roleId);
            relation.setResourceId(resourceId);
            roleResourceRelationMapper.insert(relation);
        }
        return resourceIds.size();
    }
}
